package com.worldserver.net;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class MessageDecoder extends ByteToMessageDecoder{

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf buf,
			List<Object> out) throws Exception {
		if(buf.readableBytes()<5){
			return ;
		}
		buf.markReaderIndex();
		int length=buf.readInt();
		if(buf.readableBytes()<length){
			buf.resetReaderIndex();
			return ;
		}
		byte[] b=new byte[length];
		buf.readBytes(b);
		out.add(Unpooled.buffer().writeBytes(b));
	}

}
